package com.yssoft.controller.core;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yssoft.entity.core.TDdDetail;
import com.yssoft.service.core.DdDetailService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @author 陈翼东
 * @说明 字典控制器
 * @time 2018/4/27 上午 11:23
 */
@Controller
@RequestMapping("ddDetail")
public class DdDetailController {
    
    @Resource
    private DdDetailService ddDetailService;
    
    /**
     * 计量单位字典页面
     *
     * @return
     */
    @RequestMapping("ddDetailPage")
    public String ddDetailPage(String classify, Model model) {
        model.addAttribute("classify", classify);
        switch (classify) {
            case "unit":
                model.addAttribute("name", "计量单位");
                break;
            case "workshop":
                model.addAttribute("name", "厂房(车间)");
                break;
            case "equipment":
                model.addAttribute("name", "设备");
                break;
            case "packagingSpec":
                model.addAttribute("name", "包装规格");
                break;
            case "duties":
                model.addAttribute("name", "职务");
                break;
            case "productionLine":
                model.addAttribute("name", "生产线");
                break;
            case "supplierClassify":
                model.addAttribute("name", "供应商");
                break;
            default:
                model.addAttribute("json", ddDetailService.queryMaterialDdDetailTableTree());
                return "core/ddDetail/materialsList";
        }
                return "core/ddDetail/ddDetailList";
    }
    
    /**
     * 分页查询计量单位字典
     *
     * @param page
     * @param limit
     * @param described
     * @return
     */
    @RequestMapping("queryDdDetailPage")
    @ResponseBody
    public JSONObject queryDdDetailPage(int page, int limit, String described, String classify) {
        return ddDetailService.queryDdDetailPage(page, limit, described, classify);
    }
    
    
    /**
     * 跳转到添加或者修改页面
     *
     * @param ddDetailId
     * @param classify   字典类别
     * @return
     */
    @RequestMapping("ddDetailEditPage")
    public String ddDetailEditPage(String ddDetailId, String classify, Model model,String add) {
        if (ddDetailId != null) {
            TDdDetail ddDetail = ddDetailService.queryDdDetailById(Integer.parseInt(ddDetailId));
            model.addAttribute("ddDetail", ddDetail);
        }
        switch (classify) {
            case "unit":
                model.addAttribute("name", "计量单位");
                break;
            case "workshop":
                model.addAttribute("name", "厂房(车间)");
                break;
            case "equipment":
                model.addAttribute("name", "设备");
                break;
            case "packagingSpec":
                model.addAttribute("name", "包装规格");
                break;
            case "duties":
                model.addAttribute("name", "职务");
                break;
            case "productionLine":
                model.addAttribute("name", "生产线");
                break;
            case "supplierClassify":
                model.addAttribute("name", "供应商");
                break;
            default:
               if(add!=null){
                   model.addAttribute("isAdd", true);
               }
                return "core/ddDetail/materialsEdit";
        }
        model.addAttribute("classify", classify);
       
        return "core/ddDetail/ddDetailEdit";
    }
    
    /**
     * 跳转到查看字典页面
     *
     * @param ddDetailId
     * @return
     */
    @RequestMapping("ddDetailViewPage")
    public String ddDetailViewPage(String ddDetailId, Model model) {
        if (ddDetailId != null) {
            TDdDetail ddDetail = ddDetailService.queryDdDetailById(Integer.parseInt(ddDetailId));
            model.addAttribute("ddDetail", ddDetail);
        }
        return "core/ddDetail/ddDetailView";
    }
    //-------------------------------------------------------------------------------
    
    /**
     * 保存字典信息
     *
     * @param ddDetail
     * @param id
     * @return
     */
    @RequestMapping("ddDetailSave")
    public String ddDdDetailSave(TDdDetail ddDetail, String id) {
        if (id != null && id.trim().length() > 0) {
            ddDetailService.updateDdDetail(ddDetail, id);
        } else {
            ddDetailService.addDdDetail(ddDetail);
        }
        return "redirect:ddDetailPage?classify=" + ddDetail.getClassify();
    }
    
    /**
     * 删除多条字典信息
     *
     * @param ids
     * @return
     */
    @RequestMapping("delDdDetails")
    @ResponseBody
    public Integer delDdDetails(String ids) {
        return ddDetailService.delDdDetails(ids);
    }
    
    /**
     * 删除一条字典信息
     *
     * @param id
     * @return
     */
    @RequestMapping("delDdDetail")
    @ResponseBody
    public Integer delDdDetail(String id) {
        return ddDetailService.delDdDetail(id);
    }
    
    /**
     * 查询物料的tree
     * @return
     */
    @RequestMapping("queryMaterialsTree")
    public String queryMaterialsTree(Model model){
        model.addAttribute("json",ddDetailService.queryMaterialDdDetailTreeNoChilder());
        return "core/ddDetail/materialsTree";
    }
    
    /**
     * 删除物料及物料的子集
     * @return
     */
    @RequestMapping("deleteRecursiveDdDetail")
    @ResponseBody
    public String deleteRecursiveDdDetail(String id){
        return ddDetailService.deleteRecursiveDdDetail(Integer.parseInt(id))+"";
    }
}
